package vg.services.graph_view_manager.realization.graph_view.smart_graph_view.realization;

import java.util.HashMap;
import java.util.Map;

import javax.swing.JComponent;

import org.apache.commons.lang.mutable.MutableBoolean;

import vg.services.data_base_manager.data.graph.Attribute;
import vg.services.data_base_manager.data.graph.Edge;
import vg.services.data_base_manager.data.graph.Graph;
import vg.services.data_base_manager.data.graph.Vertex;
import vg.services.graph_view_manager.interfaces.graph_view.IGraphInformation;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.interfaces.ISGVGraphView4Patching;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.interfaces.ISGVPatchManager;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.realization.patches.DefaultPatch;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.shared.Utils;

/**
 * Implementation of IGraphInformation.
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class SGVGraphInformation implements IGraphInformation {
	// Main components
	private ISGVPatchManager sgv_patch_manager;
	
	public SGVGraphInformation(ISGVPatchManager sgv_patch_manager) {
		this.sgv_patch_manager = sgv_patch_manager;
	}
	
//=============================================================================
//-----------------Methods for getting different views-------------------------
	@Override
	public JComponent getMiniMap() {
		DefaultPatch patch = new DefaultPatch("Get minimap") {
			@Override
			public void apply(ISGVGraphView4Patching graphView4patching) {
				result = graphView4patching.getMiniMap();
			}
		};
		
		Utils.add_patch2patch_manager_and_wait_result(sgv_patch_manager, patch);		
		Object result = patch.getResult();
		if (result != null)
			return (JComponent)result;
		return null;
	}

//=============================================================================
//-----------------Methods for getting information about graph elements--------		
	@Override
	public Map<Attribute, MutableBoolean> getSelectionVertexAttributes() {		
		DefaultPatch patch = new DefaultPatch("Get vertex attributes") {
			@Override
			public void apply(ISGVGraphView4Patching graphView4patching) {
				Map<Vertex, Map<Attribute, MutableBoolean>> vertices_attributes = graphView4patching.getSelectionVerticesInfo();
				Map<Attribute, MutableBoolean> buf_result = new HashMap<Attribute, MutableBoolean>();
				if (vertices_attributes != null) {
					for (Map<Attribute, MutableBoolean> buf : vertices_attributes.values()) {
						if (buf != null)
							buf_result.putAll(buf);
					}
				}
				result = buf_result;
			}
		};
		
		Utils.add_patch2patch_manager_and_wait_result(sgv_patch_manager, patch);		
		Object result = patch.getResult();
		if (result != null)
			return (Map<Attribute, MutableBoolean>)result;
		return null;
	}
	
	@Override
	public Map<Attribute, MutableBoolean> getSelectionEdgeAttributes() {
		DefaultPatch patch = new DefaultPatch("Get edge attributes") {
			@Override
			public void apply(ISGVGraphView4Patching graphView4patching) {
				Map<Edge, Map<Attribute, MutableBoolean>> vertices_attributes = graphView4patching.getSelectionEdgesInfo();
				Map<Attribute, MutableBoolean> buf_result = new HashMap<Attribute, MutableBoolean>();
				if (vertices_attributes != null) {
					for (Map<Attribute, MutableBoolean> buf : vertices_attributes.values()) {
						if (buf != null)
							buf_result.putAll(buf);
					}
				}
				result = buf_result;
			}
		};
		
		Utils.add_patch2patch_manager_and_wait_result(sgv_patch_manager, patch);		
		Object result = patch.getResult();
		if (result != null)
			return (Map<Attribute, MutableBoolean>)result;
		return null;
	}
	
	@Override
	public Graph getGraph() {
		DefaultPatch patch = new DefaultPatch("Get graph") {
			@Override
			public void apply(ISGVGraphView4Patching graphView4patching) {
				result = graphView4patching.getGraph();
			}
		};
		
		Utils.add_patch2patch_manager_and_wait_result(sgv_patch_manager, patch);		
		Object result = patch.getResult();
		if (result != null)
			return (Graph)result;
		return null;
	}
	
	@Override
	public Graph getSelectionSubGraph() {
		DefaultPatch patch = new DefaultPatch("Get selection subgraph") {
			@Override
			public void apply(ISGVGraphView4Patching graphView4patching) {
				result = graphView4patching.getSelectionSubGraph();
			}
		};
		
		Utils.add_patch2patch_manager_and_wait_result(sgv_patch_manager, patch);		
		Object result = patch.getResult();
		if (result != null)
			return (Graph)result;
		return null;
	}
}
